<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="YPgzH" id="YPgzH"><span data-lake-id="ue791e0a4" id="ue791e0a4">典型回答</span></h1>
  <p data-lake-id="u88dd353d" id="u88dd353d"><br></p>
  <p data-lake-id="ud2c0b218" id="ud2c0b218"><span data-lake-id="u646235c4" id="u646235c4">这个题其实和下面这个题基本上是一样的，解题思路也一样，就是wait-notify、lock-condition，以及Semaphore以及yeild</span></p>
  <p data-lake-id="ubb8812f4" id="ubb8812f4"><br></p>
  <p data-lake-id="u1a492067" id="u1a492067"><span data-lake-id="u94d1bf5b" id="u94d1bf5b">wait-notify：</span></p>
  <p data-lake-id="ud865c1ae" id="ud865c1ae"><span data-lake-id="u3e853873" id="u3e853873">​</span><br></p>
  <pre lang="java"><code>

public class PrintingWithWaitNotify {
    private static final Object lock = new Object();
    private static boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    for (int i = 1; i &lt;= 3; i++) {
                        while (!printNumber) {
                            lock.wait();
                        }
                        System.out.print(i);
                        printNumber = false;
                        lock.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            synchronized (lock) {
                try {
                    for (char c = 'A'; c &lt;= 'C'; c++) {
                        while (printNumber) {
                            lock.wait();
                        }
                        System.out.print(c);
                        printNumber = true;
                        lock.notify();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

</code></pre>
  <p data-lake-id="u4e9a9f79" id="u4e9a9f79"><br></p>
  <p data-lake-id="uada54820" id="uada54820"><br></p>
  <p data-lake-id="u1a6c3b8d" id="u1a6c3b8d"><span data-lake-id="ub3468017" id="ub3468017">lock-condition：</span></p>
  <pre lang="java"><code>
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintingWithReentrant {
    public static void main(String[] args) {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        Thread thread1 = new Thread(new PrintNumbers(lock, condition));
        Thread thread2 = new Thread(new PrintLetters(lock, condition));

        thread1.start();
        thread2.start();
    }
}

class PrintNumbers implements Runnable {
    private Lock lock;
    private Condition condition;

    public PrintNumbers(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        for (int i = 1; i &lt;= 3; i++) {
            lock.lock();
            try {
                System.out.print(i);
                condition.signal(); // 唤醒打印字母的线程
                if (i &lt; 3) {
                    condition.await(); // 等待打印字母的线程执行
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        }
    }
}

class PrintLetters implements Runnable {
    private Lock lock;
    private Condition condition;

    public PrintLetters(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        for (char letter = 'A'; letter &lt;= 'C'; letter++) {
            lock.lock();
            try {
                System.out.print(letter);
                condition.signal(); // 唤醒打印数字的线程
                if (letter &lt; 'C') {
                    condition.await(); // 等待打印数字的线程执行
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lock.unlock();
            }
        }
    }
}

</code></pre>
  <p data-lake-id="u15bf530b" id="u15bf530b"><br></p>
  <p data-lake-id="u7d51f290" id="u7d51f290"><span data-lake-id="u2073f21d" id="u2073f21d">Semaphore：</span></p>
  <p data-lake-id="u88ea1b48" id="u88ea1b48"><span data-lake-id="u5fc81350" id="u5fc81350">​</span><br></p>
  <pre lang="java"><code>
import java.util.concurrent.Semaphore;

public class PrintingWithSemaphore {
    private static final Semaphore semaphoreNumber = new Semaphore(1);
    private static final Semaphore semaphoreLetter = new Semaphore(0);

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            try {
                for (int i = 1; i &lt;= 3; i++) {
                    semaphoreNumber.acquire();
                    System.out.print(i);
                    semaphoreLetter.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            try {
                for (char c = 'A'; c &lt;= 'C'; c++) {
                    semaphoreLetter.acquire();
                    System.out.print(c);
                    semaphoreNumber.release();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

</code></pre>
  <p data-lake-id="ud0104b9d" id="ud0104b9d"><br></p>
  <p data-lake-id="u141392d5" id="u141392d5"><span data-lake-id="u59620c13" id="u59620c13">​</span><br></p>
  <p data-lake-id="u8764c8ab" id="u8764c8ab"><span data-lake-id="u7e86aa2e" id="u7e86aa2e">yeild：</span></p>
  <p data-lake-id="u7e2c5d66" id="u7e2c5d66"><span data-lake-id="u22f836c9" id="u22f836c9">​</span><br></p>
  <pre lang="java"><code>
public class PrintingWithYeild {
    private static volatile boolean printNumber = true;

    public static void main(String[] args) {
        Thread numberThread = new Thread(new NumberPrinter());
        Thread letterThread = new Thread(new LetterPrinter());

        numberThread.start();
        letterThread.start();
    }

    static class NumberPrinter implements Runnable {
        @Override
        public void run() {
            for (int i = 1; i &lt;= 3; i++) {
                while (!printNumber) {
                    Thread.yield();
                }
                System.out.print(i);
                printNumber = false;
                Thread.yield();
            }
        }
    }

    static class LetterPrinter implements Runnable {
        @Override
        public void run() {
            for (char c = 'A'; c &lt;= 'C'; c++) {
                while (printNumber) {
                    Thread.yield();
                }
                System.out.print(c);
                printNumber = true;
                Thread.yield();
            }
        }
    }
}

</code></pre>
 </body>
</html>